package com.sjz.demo1.controller;

import com.github.pagehelper.PageInfo;
import com.sjz.demo1.bean.User;
import com.sjz.demo1.service.UserService;
import com.sjz.demo1.tools.Results;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: SpringBoot
 * @ClassName UserController
 * @description:
 * @author: 陈真
 * @create: 2021-06-19 02:26:03
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService service;

    /**
     * 查询用户全部信息
     *
     * @return map集合
     */
    @GetMapping("/findAll")
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1",value = "pageNo") int pageNo,int pageSize) {
        /*创建map集合，10是集合的初始大小，可以不写*/
        Map<String, Object> map = new HashMap<>(10);
        /*调用service层的查询全部的方法，传入pageNo, pageSize 这两个参数*/
        PageInfo<User> info = service.findAll(pageNo, pageSize);
        /*集合数据*/
        map.put("user_list", info.getList());
        /*总记录数*/
        map.put("total",info.getTotal());
        /*返回map集合*/
        return map;
    }
    @GetMapping("/findAll2")
    public List<User> findAll2(){
        return service.findAll2();
    }

    @PostMapping("/saveUser")
    public Results saveUser(@RequestBody User user) {
        Results results = new Results();
        try {
            if(user.getId()==null){
                System.out.println("进行保存");
                service.saveUser(user);
                results.setMessage("保存成功");
            }else{
                System.out.println("进行编辑");
                service.updateUserById(user);
                results.setMessage("编辑成功");
            }
            results.setStatus(true);
        } catch (Exception e) {
            results.setStatus(false);
            results.setMessage("保存失败");
        }
        return results;
    }

    /**
     * 根据id删除用户
     *
     * @param id 编号
     * @return 影响的行数
     */
    @GetMapping("/delUserById")
    public Results delUserById(int id) {
        Results results = new Results();
        try {
            service.delUserById(id);
            results.setStatus(true);
            results.setMessage("删除成功");
        } catch (Exception e) {
            results.setStatus(false);
            results.setMessage("删除失败");
        }
        return results;
    }

    /**
     * 根据编号查询用户信息
     *
     * @param id 编号
     * @return user对象
     */
    @GetMapping("/findUserById")
    public User findUserById(int id) {
        User user = service.findUserById(id);
        return user;
    }
    @RequestMapping(value = "/loginByName",method = RequestMethod.POST)
    public Results loginByName(@RequestBody User user){
        Results results = new Results();
        try {
            service.loginByName(user);
            results.setStatus(true);
            results.setMessage("登录成功");
        } catch (Exception e) {
            results.setStatus(false);
            results.setMessage("登录失败");
        }
        return results;
    }
}
